var orderinfo = {};
var payType='';
var orderId;
var ordertype;  //订单类型

apiready = function () {

   orderinfo = api.pageParam.orderinfo;
   orderId = orderinfo.orderNo;
   ordertype=orderinfo.ordertype;
   payType=orderinfo.payType;
   
   if(ordertype=='ZX'){
       
       $api.text($api.byId('ordernospan'),'预约单号');
       $api.text($api.byId('tccategory'), orderinfo.category);
       $api.text($api.byId('totalspan'),'预付定金');
       
       $api.css($api.byId('tccategorydiv'),'display:block');
       $api.css($api.byId('paymentdiv'),'display:block');
       
   }
   
   showPayInfo();
	
}

//显示订单信息
var showPayInfo = function(){
	$api.text($api.byId('orderno'), orderinfo.orderNo);
	//$api.text($api.byId('ordertotal'), orderinfo.total.toFixed(2));
	$api.text($api.byId('ordertotal'), orderinfo.total);
	$api.text($api.byId('payType'), orderinfo.payType);
	
//	if(payType=='支付宝'){
//	     $api.attr($api.byId('paytype1'), 'checked', 'checked');
//	     $api.removeAttr($api.byId('paytype2'), 'checked');
//	     
//	     
//	}else if(payType=='微信支付'){
//	    $api.attr($api.byId('paytype2'), 'checked', 'checked');
//	     $api.removeAttr($api.byId('paytype1'), 'checked');
//	}
}

//选择支付方式
function checkPaytype(type){
    payType=type;
    
    if(type=='支付宝'){
	     $api.attr($api.byId('paytype1'), 'checked', 'checked');
	     $api.removeAttr($api.byId('paytype2'), 'checked');
	     
	}else if(type=='微信支付'){
	    $api.attr($api.byId('paytype2'), 'checked', 'checked');
	     $api.removeAttr($api.byId('paytype1'), 'checked');
	}
	
	var order = {
	     'orderNo':orderinfo.orderNo,
	     'payType':payType,
	};
	
	//修改支付方式
	do_ajax('/app/orderpaytype/update','post',order,function(respose,err){
	        
        //发送订单改变事件
        api.sendEvent({
		    name: 'changeOrder',
		});
        
		api.hideProgress();
	});
	
};

//订单支付
function orderPay(){
   if(payType=='支付宝'){
        alipay();
   }
   
   if(payType=='微信支付'){
   		wechatpay();
   }
};

/*----------------------支付宝支付------------------------*/
function alipay(){
      api.showProgress({
            style: 'default',
            animationType: 'fade',
            title: '正在加载',
            text: '请稍候',
            modal: false
        });
        
        api.ajax({
            url: $api.getStorage('serviceUrl') + '/alipay/app/order?orderNo=' + orderId,
            method: 'get',
            timeout: 30,
            dataType: 'json',
            returnAll:false
            
        },function(ret,err){
//			alert(JSON.stringify(ret));
            
            if (ret.resultCode==0) {
            
            	var aliPay = api.require('aliPay');
            	
                aliPay.pay({
			    	subject:ret.datas.subject,
			    	body:ret.datas.body,
			    	amount:ret.datas.amount,
			    	tradeNO:ret.datas.tradeNO
			    	
				},function(result,err) {
                	api.hideProgress();
                	
                	//支付成功
                	if(result.code == '9000'){
				
						api.alert({
							title: '支付结果',
							msg: '支付成功',
							buttons: ['确定']
						});
				     
					    //发送订单改变事件
				        api.sendEvent({
						    name: 'changeOrder',
						});
					    
					    api.closeWin();
					
					//取消支付
					}else if(result.code == '6001'){
						//取消操作
						api.alert({
						    title: '支付结果',
						    msg: '支付取消',
						    buttons: ['确定']
						});
					
					//支付失败
					}else{
					     api.alert({
					        title: '支付结果',
					        msg: result.msg,
					        buttons: ['确定']
					    });
					}
                });
                
            }else {
                api.hideProgress();
                api.toast({
                    msg: ret.datas,
                    duration:1000,
                    location: 'top'
                });
            }
        });
};


/*----------------------微信支付------------------------*/
function wechatpay(){

    api.ajax({
            url: $api.getStorage('serviceUrl') + '/wxpay/app/order?orderNo=' + orderId,
            method: 'get',
            timeout: 30,
            dataType: 'json',
            returnAll:false
            
        },function(ret,err){
//			alert(JSON.stringify(ret));
            
            if (ret) {
                var wxPay = api.require('wxPay');
                
                wxPay.payOrder({
				    apiKey: ret.appid,
				    orderId: ret.prepayid,
				    mchId: ret.partnerid,
				    nonceStr: ret.noncestr,
				    timeStamp: ret.timestamp,
				    'package': 'Sign=WXPay',
				    sign: ret.sign
				}, function(ret, err){
				     if(ret.status){
				        //支付成功
                        api.alert({
                            title: '支付结果',
                            msg: '支付成功',
                            buttons:['确定']
                        });
                        
                        api.closeWin();
				     }else{
				        if(err.code==-2){
				        	api.alert({
	                            title: '支付结果',
	                            msg: '取消支付',
	                            buttons:['确定']
	                        });
				        }else{
				        	api.alert({
	                            title: '支付结果',
	                            msg: '支付失败',
	                            buttons:['确定']
	                        });
				        }
				    }
				});
                api.hideProgress();
                
            }else {
                api.hideProgress();
                alert(err);
                api.toast({
                    msg: '下单失败',
                    duration:1000,
                    location: 'top'
                });
            }
        });
};

